package com.handong.handserialprogram.utils;


import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.handong.handserialprogram.constants.HttpConstant;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.conn.ssl.SSLSocketFactory;


/**
 * Created by Administrator on 2016/12/16.
 */

public class AsyncHttpUtil {

    private static AsyncHttpClient mHttpClient;

    private AsyncHttpUtil() {
    }


        static {

            mHttpClient = new AsyncHttpClient(true,80,443);
            //mHttpClient.setSSLSocketFactory(sf);
            mHttpClient.setConnectTimeout(20 * 1000);
            mHttpClient.setResponseTimeout(25 * 1000);
            mHttpClient.setMaxRetriesAndTimeout(1, 1500);
    }

    private static String formatBaseUrl() {
        StringBuilder builder = new StringBuilder(50);
        builder.append(HttpConstant.HTTPS_HEAD);
        builder.append(SPUtil.getStringValue(SPUtil.SERVER_IP));
        builder.append(":" + SPUtil.getIntValue(SPUtil.MISSION_PORT));
        builder.append(HttpConstant.ACTION_REQUEST);
        return builder.toString();
    }

    /**
     * 判断是否有可用的网络。
     *
     * @param context
     * @return
     */
    public static boolean isNetworkConnected(Context context) {
        return isNetworkConnected(context, true);
    }

    public static boolean isNetworkConnected(Context context, boolean isToast) {
        if (context != null) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context
                    .getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager
                    .getActiveNetworkInfo();
            if (networkInfo != null) {
                return networkInfo.isAvailable();
            }
        }
        if (isToast) {
            AppUtil.showShortToast(context, "请检测网络是否连通");
        }
        return false;
    }

    /**
     * 判断是不是wifi网络。
     *
     * @param paramContext
     * @return
     */
    public static boolean isWifiConnected(Context paramContext) {
        NetworkInfo localNetworkInfo = ((ConnectivityManager) paramContext.getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        return (localNetworkInfo != null) && (localNetworkInfo.getState() == NetworkInfo.State.CONNECTED);
    }

    public static boolean isMobileConnected(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        return info != null && info.getState() == NetworkInfo.State.CONNECTED;
    }

    public static void getRequest(String url, AsyncHttpResponseHandler handler) {
        mHttpClient.get(url, handler);
    }

    public static void getRequest(RequestParams params, AsyncHttpResponseHandler handler) {
        mHttpClient.get(formatBaseUrl(), params, handler);
    }

    public static void postRequest(String url, AsyncHttpResponseHandler handler) {
        mHttpClient.post(url, handler);
    }

    public static void postRequest(RequestParams params, AsyncHttpResponseHandler handler) {
        mHttpClient.post(formatBaseUrl(), params, handler);
    }

    public static void postSpecifiedRequest(Context context, RequestParams params, AsyncHttpResponseHandler handler) {
        mHttpClient.post(context, formatBaseUrl(), params, handler);
    }

    public static void cancelSpecifiedRequest(Context context) {
        mHttpClient.cancelRequests(context, true);
    }
}
